#!/bin/bash
#临时证书生成

generate_certificate() {
  # 设置变量
  local COMMON_NAME="$1"
  local CERT_DIR="$2"
  local DAYS=${3:-365} # 默认证书有效期为365天

  # 生成私钥
  openssl genrsa -out "$CERT_DIR/private.key" 2048

  # 生成证书签名请求（CSR）
  openssl req -new -key "$CERT_DIR/private.key" -out "$CERT_DIR/certificate.csr" -subj "/CN=$COMMON_NAME"

  # 生成自签名证书
  openssl x509 -req -in "$CERT_DIR/certificate.csr" -signkey "$CERT_DIR/private.key" -out "$CERT_DIR/certificate.crt" -days "$DAYS"

  # 输出成功信息
  echo "HTTPS证书生成成功！"
  echo "私钥文件路径：$CERT_DIR/private.key"
  echo "证书文件路径：$CERT_DIR/certificate.crt"
}

# 调用函数并传递相应参数
generate_certificate "wangwuliguagua.com" "/sumz/"

openssl base64 -in certificate.crt -out certificate_base64.txt

openssl base64 -in private.key -out private_key_base64.txt

cat certificate_base64.txt && cat private_key_base64.txt
